using System.Collections.Concurrent;
using BattleFury.Lib.Bots;
using BattleFury.Lib.GameEvents;
using BattleFury.Lib.Network.Packets;
using Microsoft.Xna.Framework;

namespace BattleFury.Lib.ServerPlayer
{
    public class GenericPlayer
    {

        public bool UpdatePosition(double timeIncrement, Vector2 position)
        {
            if (CurrentMovement != null && CurrentMovement.IsMoving)
            {
                Position = CurrentMovement.UpdatePosition(timeIncrement, position);
                if (Position == position)
                {
                    //Stop movement because the last update didn't move us
                    Move(new Movement());
                    return false;
                }
                return true;
            }
            return false;
        }
        public GenericPlayer(long Id)
        {
            this.Id = Id;
            //CurrentMovement = new Movement();
        }

        protected void Move(Movement mv)
        {
            if (MovementChanged == null)
                return;
            CurrentMovement = mv;
            MovePacket mov = new MovePacket();
            mov.SourceObjectId = Id;
            mov.Destination = CurrentMovement.Destination;
            mov.Velocity = CurrentMovement.Velocity;

            MovementChanged(this, new MovementChangedEventArge() { MovementPacket = mov });
        }
        public event MovementChangedEventHandler MovementChanged;

        public readonly long Id;

        private Movement _currentMovement;

        public Movement CurrentMovement
        {
            get { return _currentMovement; }
            set
            {
                _currentMovement = value;
                if (MovementChanged == null)
                    return;
                MovePacket mov = new MovePacket();
                mov.SourceObjectId = Id;
                mov.Destination = CurrentMovement.Destination;
                mov.Velocity = CurrentMovement.Velocity;
                MovementChanged(this, new MovementChangedEventArge() { MovementPacket = mov });
            }
        }

        private Vector2 _position;

        public Vector2 Position
        {
            get { return _position; }
            set
            {
                if (_position != value)
                { _position = value; }
            }
        }

        public Vector2 Size { get; set; }
        public Rectangle Bounds { get { return new Rectangle((int)Position.X, (int)Position.Y, (int)Size.X, (int)Size.Y); } }
    }

    public delegate void MovementChangedEventHandler(GenericPlayer player, MovementChangedEventArge e);

}